home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 423 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.5 KB  |  96 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: BXHG48B@prodigy.com (Karen Erner)
  3. Newsgroups: comp.std.c++
  4. Subject: vector<T,Allocator> insert functions
  5. Date: 20 Feb 1996 10:47:09 PST
  6. Organization: Prodigy Services Company  1-800-PRODIGY
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4g892r$1ave@usenetp1.news.prodigy.com>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 18 Feb 1996 22:26:35 GMT
  11. X-Newsreader: Version 1.2
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMSoXO0y4NqrwXLNJAQG9LwH/cPoLkVDllEw2C2GCYxcOHDtv/IPZpY/x
  14.     A05EYfXTbNXaHRhgIIEO/zLxhCy8hBdctI4da7JuHoJ2K0X6frj8Iw==
  15.     =LKAm
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. In attempting to implement the vector<T, Allocator> class, I have two 
  19. questions about the insert() functions.  I am working from the April 1995 
  20. working draft, so I apologize if any of my questions have already been 
  21. resolved.
  22.  
  23. Under subclause 23.2.5.6 vector modifiers, we have rules for the insert() 
  24. functions.  I read the following:
  25.  
  26. "Notes:  Causes reallocation if the new size is greater than the old 
  27. capacity.  If no reallocation happens, all the iterators and references 
  28. before the insertion point remain valid."
  29.  
  30. What is not clear to me, and this is probably perfectly obvious, but what 
  31. insertion points are allowed?  This is my first question.  As a minimum, 
  32. I guess that begin() <= position <= end().  Are insertion points allowed 
  33. outside of these values?  I ask this because we are allowed to reserve 
  34. and reallocate memory.  For example, here is a case where the new size 
  35. does not exceed the capacity, and the capacity exceeds the old size.  Let 
  36. us also say that we have some print function that writes values from 
  37. begin() to end() to standard output, and that ## denotes denotes the 
  38. result of printing uninitialized memory.
  39.  
  40. vector<int allocator> V(3);
  41. cout << V.size() << endl;          // prints 3
  42. cout << V.capacity() << endl;    // prints 3
  43. V.print();                                    // prints 0 0 0
  44.  
  45. V.reserve(5);
  46. cout << V.size() << endl;          // prints 3
  47. cout << V.capacity() << endl;    // prints 5
  48. V.print();                                    // prints 0 0 0
  49.  
  50. Can we insert a value at the new fifth position?
  51.  
  52. V.insert(V.end() + 2, 12);          // insert 12 at 5th position
  53. V.print();                                 // prints 0 0 0 ## 12
  54.  
  55. Or a value before the first position?
  56.  
  57. V.insert(V.begin() - 1, 12);          // insert 12 before 1st position
  58. V.print();                                 // 12 ##  0 0 0
  59.  
  60. If these cases are permissable, would it also be allowable to use these 
  61. insertion points without previously reserving the required space forcing 
  62. a reallocation?
  63.  
  64. As my second question, I see that we have:
  65.  
  66. iterator insert(iterator position, const T& x = T());
  67. void insert(iterator position, size_type n, const T& x);
  68. template <class T>
  69. void insert(iterator position, InputerIterator first, InputIterator last);
  70.  
  71.  
  72. Shouldn't we also be able to insert vectors, like
  73.  
  74. void insert(iterator position, const vector<T,
  75.      Allocator>& v);
  76.  
  77. .. or better still ...
  78.  
  79. template <Allocator2>
  80. void insert(iterator position,
  81.      const vector<T, Allocator2>& v);  ?
  82.  
  83. I apologize once again, if I am asking questions gone over before.
  84.  
  85.           Karen
  86.     
  87.  
  88.     
  89. ---
  90. [ To submit articles: Try just posting with your newsreader.  If that fails,
  91.                       use mailto:std-c++@ncar.ucar.edu
  92.   FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  93.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  94.   Comments? mailto:std.c++-request@ncar.ucar.edu 
  95. ]
  96.